package com.sxhuayuan.parking.controller.admin;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.entity.ParkingLotUser;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.ParkingLotUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 车场用户
 * 
 * @author 何龙
 * @create 2019-04-19 11:23
 */
@RestController("adminParkingLotUserController")
@RequestMapping("/admin/parkinglotuser")
public class ParkingLotUserController extends BaseController {
	@Resource(name = "parkingLotUserServiceImpl")
	ParkingLotUserService service;

	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService parkingLotService;

	/** 查询分页列表 */

	@PostMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable) {
		Page<ParkingLotUser> page = service.findPage(pageable);
		return successResp(page);
	}

	/** 删除用户 */
	@RequestMapping("/delete")
	public RespMessage deleteAgent(Long id) {
		if (Objects.isNull(id)) {
			return invalidToken();
		}
		service.delete(id);
		return RespMessage.success();

	}

	/** 添加停车场用户 */
	@RequestMapping("/save")
	public RespMessage save(@Validated @RequestBody ParkingLotUser parkingLotUser, HttpServletRequest request) {

		if (service.usernameExists(parkingLotUser.getUsername())) {
			return RespMessage.builder().validError().setErr(Message.FORM_UNIQUE_INVALID).build();
		}
		Set<ParkingLot> parkingLots = parkingLotUser.getParkingLots();
		Set<ParkingLot> set = new HashSet<>();
		for (ParkingLot parking : parkingLots) {
			ParkingLot one = parkingLotService.findOne(parking.getId());
			if (one == null) {
				return RespMessage.error("车场不存在");
			}
			set.add(one);
		}
		parkingLotUser.setParkingLots(set);
		service.save(parkingLotUser);
		return RespMessage.success();
	}

	/**
	 * 锁定 启用
	 */
	@RequestMapping("/locked/or/enabled")
	public RespMessage onIsLockedAndEnabled(Long id, String type, Boolean value) {
		if (Objects.isNull(id) || !StringUtils.isNotBlank(type) || Objects.isNull(value)) {
			return RespMessage.error(RespMessage.Status.VALID_ERR, Message.FORM_VALID_FAIL);
		}
		service.onIsLockedAndEnabled(id, type, value);
		return RespMessage.success();

	}

	@PostMapping("/update")
	public RespMessage update(@Validated @RequestBody ParkingLotUser parkingLotUser) {
		ParkingLotUser one = service.findOne(parkingLotUser.getId());

		one.setName(parkingLotUser.getName());
		one.setUsername(parkingLotUser.getUsername());
		one.setRoles(parkingLotUser.getRoles());
		Set<ParkingLot> set = new HashSet<>();
		Set<ParkingLot> parkingLots = parkingLotUser.getParkingLots();
		for (ParkingLot parkingLot : parkingLots) {
			ParkingLot lot = parkingLotService.findById(parkingLot.getId());
			if (lot==null){
				return RespMessage.error("选择的车场不存在,请刷新重试");
			}
			set.add(lot);
		}
		one.setParkingLots(set);
		if (parkingLotUser.getPassword() != null) {
			one.setPassword(parkingLotUser.getPassword());
		}
		service.update(one);
		return RespMessage.success();
	}

}
